<?php

declare(strict_types=1);

namespace App\Controller\Api;
use Hyperf\HttpServer\Annotation\AutoController;

use Hyperf\Redis\Redis;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;

#[AutoController]
class PassportController extends AbstractController
{
    protected Redis $redis;
    
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return $this->returnSuccess(['method' => $method, 'message' => 'hello'], '成功');
    }
    
    public function captcha()
    {
        $phraseBuilder = new PhraseBuilder(4, '0123456789');
        $builder = new CaptchaBuilder(null,$phraseBuilder);
        $builder->build();
        $builder->setBackgroundColor(220, 210, 230);
        $captcha = $builder->inline();  //输出base64格式图片
        $code = $builder->getPhrase();  //验证码

        // 生成唯一的key标识符
        $key = uniqid('captcha');
        $this->redis->set('phrase_'.$key, $code, 120);
        
        return $this->returnSuccess(compact('captcha','code','key'), '成功');
    }
}
